package com.ruoyi.auth.service;

import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.constant.UserConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.enums.UserStatus;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.ServletUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.ip.IpUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.RemoteBankUserService;
import com.ruoyi.system.api.RemoteLogService;
import com.ruoyi.system.api.RemoteUserService;
import com.ruoyi.system.api.domain.BankUser;
import com.ruoyi.system.api.domain.SysLogininfor;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 登录校验方法
 * 
 * @author ruoyi
 */
@Component
public class MiniProgramLoginService
{
    @Autowired
    private RemoteLogService remoteLogService;

    @Autowired
    private RemoteBankUserService remoteUserService;

    /**
     * 登录
     */
    public LoginUser login(String openId) {
        // 用户名或密码为空 错误
        if (StringUtils.isBlank(openId))
        {
            recordLogininfor(openId, Constants.LOGIN_FAIL, "openId必须填写");
            throw new ServiceException("openId必须填写");
        }

        // 查询用户信息
        R<LoginUser> userResult = remoteUserService.getUserInfo(openId, SecurityConstants.INNER);

        if (R.FAIL == userResult.getCode())
        {
            throw new ServiceException(userResult.getMsg());
        }

        if (StringUtils.isNull(userResult) || StringUtils.isNull(userResult.getData()))
        {
            recordLogininfor(openId, Constants.LOGIN_FAIL, "登录用户不存在");
            register(openId); // 用户不存在则自动注册
            userResult = remoteUserService.getUserInfo(openId, SecurityConstants.INNER);
        }
        LoginUser userInfo = userResult.getData();

        recordLogininfor(openId, Constants.LOGIN_SUCCESS, "登录成功");
        return userInfo;
    }

    public void logout(String loginName)
    {
        recordLogininfor(loginName, Constants.LOGOUT, "退出成功");
    }

    /**
     * 注册
     */
    public void register(String openId)
    {
        // 注册用户信息
        BankUser user = new BankUser();
        user.setOpenId(openId);
        R<?> registerResult = remoteUserService.registerUserInfo(user, SecurityConstants.INNER);

        if (R.FAIL == registerResult.getCode())
        {
            throw new ServiceException(registerResult.getMsg());
        }
        recordLogininfor(openId, Constants.REGISTER, "注册成功");
    }

    /**
     * 记录登录信息
     * 
     * @param openId 用户名
     * @param status 状态
     * @param message 消息内容
     * @return
     */
    public void recordLogininfor(String openId, String status, String message)
    {
        SysLogininfor logininfor = new SysLogininfor();
        logininfor.setUserName(openId);
        logininfor.setIpaddr(IpUtils.getIpAddr(ServletUtils.getRequest()));
        logininfor.setMsg(message);
        // 日志状态
        if (StringUtils.equalsAny(status, Constants.LOGIN_SUCCESS, Constants.LOGOUT, Constants.REGISTER))
        {
            logininfor.setStatus(Constants.LOGIN_SUCCESS_STATUS);
        }
        else if (Constants.LOGIN_FAIL.equals(status))
        {
            logininfor.setStatus(Constants.LOGIN_FAIL_STATUS);
        }
        remoteLogService.saveLogininfor(logininfor, SecurityConstants.INNER);
    }
}